package view.mediator
{
	import com.google.maps.Map;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.MapZoomEvent;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.UIComponent;
	
	import controllers.commands.QueryDevicesCommand;
	import controllers.commands.StartElectricBudryCommand;
	import controllers.managers.SingletonManager;
	import controllers.managers.StateManager;
	import controllers.managers.UIInstanceManager;
	
	import model.context.ElecBoundaryContext;
	import model.context.base.IContext;
	import model.states.MaperState;
	
	import utils.MathUtil;
	
	import view.mediator.base.IMediator;
	import view.panel.ElectricBoundaryToolbar;
	import view.panel.LocationInfoPanel;

	/**
	 * 1.将地图处理事件集中放到此Mediator中处理
	 * 2.事件由此统一处理
	 **/
	public class MapUIEventMediator implements IMediator
	{
		public static var NAME:String="ElectricBoundaryMediator/";
		private var map:Map = null;
		private var cmdEBoundary:StartElectricBudryCommand = null;
				
		private var uiInst:UIInstanceManager = null;
		private var infoPanel:LocationInfoPanel = null;
		private var toolBar:ElectricBoundaryToolbar = null;		
		private var mgr:StateManager  = null; 
		private var eleContext : ElecBoundaryContext = null;
		
		public function MapUIEventMediator()
		{
		}

		public function setContext(context:IContext):void
		{}
		
		public function setView(view:UIComponent):void
		{
			map = Map(view);
			eleContext  = new ElecBoundaryContext();
			
			//准备command
			cmdEBoundary = new StartElectricBudryCommand(map);
			
			//获取UI控件
			uiInst = UIInstanceManager(SingletonManager.getServ(UIInstanceManager.NAME));
			infoPanel = LocationInfoPanel(uiInst.getView(LocationInfoPanel.NAME));
			toolBar = ElectricBoundaryToolbar(uiInst.getView(ElectricBoundaryToolbar.NAME));
			
			//准备toolbar的mediator
			var eleToolbarMediator : ElectricBoundaryToolMediator = new ElectricBoundaryToolMediator();
			eleContext.map = map;
			eleToolbarMediator.setContext( eleContext );
			eleToolbarMediator.setView(toolBar);
			
			/*如果是当前状态是电子围栏，则屏蔽鼠标冒泡事件*/
			mgr  = StateManager(SingletonManager.getServ(StateManager.NAME));
			
			initEvents();
		}
		
		/**
		 * 注册事件
		 **/
		public function initEvents():void
		{
			//数据准备完毕，开始侦听器
			map.addEventListener(MapMouseEvent.MOUSE_DOWN, onMapMouseDownHandler);
			map.addEventListener(MapMouseEvent.MOUSE_UP, onMapMouseUpHandler);
			map.addEventListener(MapMouseEvent.MOUSE_MOVE, onMonitLocationHandler);
			map.addEventListener(MapZoomEvent.ZOOM_CHANGED, onUpdateNewMarkLocation);
		}
		
		/**
		 *  重新更新位置
		 * */
		private function onUpdateNewMarkLocation(evt:Event):void
		{
			//新增当地图改变大小的时候，重新更新位置...
			var cmd:QueryDevicesCommand 
										= QueryDevicesCommand(SingletonManager.getServ(QueryDevicesCommand.NAME));
			if ( cmd != null ) 
			{
				cmd.execute();
			}
		}
		
		private function onMapMouseUpHandler(evt:MapMouseEvent):void
		{
			infoPanel.visible = false;
			map.removeEventListener(MouseEvent.MOUSE_MOVE, onStartDragMapHandler);
			
			if( cmdEBoundary != null && mgr.getState() == MaperState.ELECTRIC_BOUNDARY)
			{
				cmdEBoundary.onMUpHandler(evt.latLng);
			}
		}
		
		private function onMapMouseDownHandler(evt:MapMouseEvent):void
		{
			
			//显示到位置信息板
			infoPanel.setStartInfo( MathUtil.formatLocation(evt.latLng.lat())  +  "," +MathUtil.formatLocation( evt.latLng.lng()));
			
			//侦听鼠标的事件
			map.addEventListener(MouseEvent.MOUSE_MOVE, onStartDragMapHandler);
			
			if( cmdEBoundary != null && mgr.getState() == MaperState.ELECTRIC_BOUNDARY)
			{
				toolBar.visible = true;
				//设置上下文
				eleContext.startLatlnt = evt.latLng;
				cmdEBoundary.onMDownHandler(evt.latLng );
			}
		}
		
		/*监视当前坐标*/
		private function onMonitLocationHandler(evt:MapMouseEvent):void
		{
			infoPanel.setEndInfo(MathUtil.formatLocation(evt.latLng.lat())  +  "," + MathUtil.formatLocation(evt.latLng.lng()));
			if( cmdEBoundary != null && mgr.getState() == MaperState.ELECTRIC_BOUNDARY)
			{
				//设置最大经纬度上下文
				eleContext.endLatlnt = evt.latLng;
				cmdEBoundary.onMMovingHandler(evt.latLng);
			}
			
			if (mgr != null && mgr.getState() == MaperState.ELECTRIC_BOUNDARY) {
				evt.stopPropagation();
				this.map.disableDragging();
			}
			else
			{
				this.map.enableDragging();
			}
		}
		
		private function onStartDragMapHandler(evt:MouseEvent):void
		{
			if (mgr != null && mgr.getState() == MaperState.ELECTRIC_BOUNDARY) {
				infoPanel.x = evt.localX + 25;
				infoPanel.y = evt.localY + infoPanel.height + 5;
				infoPanel.visible = true;
			}	
		}
	}
}
